package com.youkeyi.ddy.cloud.application.business.storage.stock.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.storage.stock.BusinessStockCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.stock.dto.BusinessStockListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.stock.dto.BusinessStockListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.stock.param.BusinessStockCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.stock.param.BusinessStockListCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.StorageStockCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.dto.StorageStockListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.StorageStockCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.StorageStockQueryCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessStockController
 *
 * @Author liuxiawang
 * @Date 2024/3/28 10:20 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessStockController implements BusinessStockCloudService {
    
    @Autowired
    private StorageStockCloudService storageStockCloudService;

    @Override
    public BusinessStockListCloudDto list(BusinessStockListCloudParam param) {
        BusinessStockListCloudDto dto = new BusinessStockListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        StorageStockQueryCloudParam stockQueryCloudParam = new StorageStockQueryCloudParam();
        stockQueryCloudParam.setStockPrimaryKeys(param.getStockPrimaryKeys());
        stockQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        stockQueryCloudParam.setBillNos(param.getBillNos());
        stockQueryCloudParam.setBarCodes(param.getBarCodes());
        stockQueryCloudParam.setGoodsKeys(param.getGoodsKeys());
        stockQueryCloudParam.setGoodsNameFuzzy(param.getGoodsNameFuzzy());
        stockQueryCloudParam.setKeyWordFuzzy(param.getKeyWordFuzzy());
        stockQueryCloudParam.setPageSize(param.getPageSize());
        stockQueryCloudParam.setCurrentPage(param.getCurrentPage());
        StorageStockListCloudDto stockDtos = storageStockCloudService.query(stockQueryCloudParam);
        if (null != stockDtos && CollectionUtils.isNotEmpty(stockDtos.getRows())) {
            List<BusinessStockListItemCloudDto> items = new ArrayList<>();
            stockDtos.getRows().forEach(stock -> {
                BusinessStockListItemCloudDto item = new BusinessStockListItemCloudDto();
                item.setStockPrimaryKey(stock.getStockPrimaryKey());
                item.setCorpKey(stock.getCorpKey());
                item.setBillNo(stock.getBillNo());
                item.setWarehouseKey(stock.getWarehouseKey());
                item.setWarehouseName(stock.getWarehouseName());
                item.setBarCode(stock.getBarCode());
                item.setGoodsKey(stock.getGoodsKey());
                item.setGoodsName(stock.getGoodsName());
                item.setKeyWord(stock.getKeyWord());
                item.setUnitKey(stock.getUnitKey());
                item.setUnitName(stock.getUnitName());
                item.setProductDate(stock.getProductDate());
                item.setExpirationDate(stock.getExpirationDate());
                item.setShelfKey(stock.getShelfKey());
                item.setQuantity(stock.getQuantity());
                item.setPrice(stock.getPrice());
                item.setAdvicePrice(stock.getAdvicePrice());
                item.setSupplierKey(stock.getSupplierKey());
                item.setSupplierName(stock.getSupplierName());
                item.setInStoreType(stock.getInStoreType());
                item.setState(stock.getState());
                item.setCooperationMode(stock.getCooperationMode());
                item.setOrigStockKey(stock.getOrigStockKey());
                item.setNotes(stock.getNotes());
                item.setPurchaseUserKey(stock.getPurchaseUserKey());
                item.setStorageUserKey(stock.getStorageUserKey());
                item.setStorageTime(stock.getStorageTime());
                item.setFromType(stock.getFromType());
                item.setPayType(stock.getPayType());
                item.setCreateUserKey(stock.getCreateUserKey());
                item.setCreateUserName(stock.getCreateUserName());
                item.setCreateTime(stock.getCreateTime());
                item.setUpdateUserKey(stock.getUpdateUserKey());
                item.setUpdateUserName(stock.getUpdateUserName());
                item.setUpdateTime(stock.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(stockDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessStockCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getGoodsKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageStockCreateCloudParam stockCreateCloudParam = new StorageStockCreateCloudParam();
        stockCreateCloudParam.setCorpKey(corpKey);
        stockCreateCloudParam.setBillNo(param.getBillNo());
        stockCreateCloudParam.setWarehouseKey(param.getWarehouseKey());
        stockCreateCloudParam.setWarehouseName(param.getWarehouseName());
        stockCreateCloudParam.setBarCode(param.getBarCode());
        stockCreateCloudParam.setGoodsKey(param.getGoodsKey());
        stockCreateCloudParam.setGoodsName(param.getGoodsName());
        stockCreateCloudParam.setKeyWord(param.getKeyWord());
        stockCreateCloudParam.setUnitKey(param.getUnitKey());
        stockCreateCloudParam.setUnitName(param.getUnitName());
        stockCreateCloudParam.setProductDate(param.getProductDate());
        stockCreateCloudParam.setExpirationDate(param.getExpirationDate());
        stockCreateCloudParam.setShelfKey(param.getShelfKey());
        stockCreateCloudParam.setQuantity(param.getQuantity());
        stockCreateCloudParam.setPrice(param.getPrice());
        stockCreateCloudParam.setAdvicePrice(param.getAdvicePrice());
        stockCreateCloudParam.setSupplierKey(param.getSupplierKey());
        stockCreateCloudParam.setSupplierName(param.getSupplierName());
        stockCreateCloudParam.setInStoreType(param.getInStoreType());
        stockCreateCloudParam.setState(param.getState());
        stockCreateCloudParam.setCooperationMode(param.getCooperationMode());
        stockCreateCloudParam.setOrigStockKey(param.getOrigStockKey());
        stockCreateCloudParam.setNotes(param.getNotes());
        stockCreateCloudParam.setPurchaseUserKey(param.getPurchaseUserKey());
        stockCreateCloudParam.setStorageUserKey(param.getStorageUserKey());
        stockCreateCloudParam.setStorageTime(param.getStorageTime());
        stockCreateCloudParam.setFromType(param.getFromType());
        stockCreateCloudParam.setPayType(param.getPayType());
        stockCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        stockCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return storageStockCloudService.create(stockCreateCloudParam);
    }
}
